package com.chaodev.base.controller;


import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.model.*;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.UserQueryDTO;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.service.IUserService;
import com.chaodev.base.service.VolunteerService;
import com.chaodev.base.utils.Base64EnOut;
import com.chaodev.base.utils.JwtUtil;
import com.chaodev.base.utils.RequestParamerMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping(value = "/users")
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private IUserService userService;

    @Autowired
    private VolunteerService volunteerService;

    @Value("${myserver.name}")
    private String myServerName;

    /**
     * @Description: 获取用户列表
     * @Author: xy
     * @CreateDate: 2019/12/28 11:07
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:07
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/getUserList")
    @ApiOperation(value = "获取用户列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result userQuery(@RequestBody @ApiParam(value = "分页传入参数") DataPageVO dataPage, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
//            Map<String, Object> map = new HashMap<>();
//            map.put("P_PAGE", dataPage.getPage());
//            map.put("P_PAGESIZE", dataPage.getRows());
//            map.put("P_KEYWORD", dataPage.getKeyword());
//            map.put("P_SORTNAME", dataPage.getSort());
//            map.put("P_SORTORDER", dataPage.getOrder());
//            map.put("P_USER_ID", dataPage.getUserId());
//            map.put("P_ROLE_ID", dataPage.getRoleId());
//            map.put("P_USER_NAME", dataPage.getUserName());
//            map.put("P_DEPART_ID", dataPage.getDepartId());
//            map.put("U_DEPART_ID", userInfo.getDepartId());
            if (null == dataPage.getPage()) {
                dataPage.setPage(1);
            }
            if (null == dataPage.getRows()) {
                dataPage.setRows(20);
            }
            if (!StringUtils.hasText(dataPage.getSort())) {
                dataPage.setSort(" CREATE_TIME ");
            }
            if (!StringUtils.hasText(dataPage.getOrder())) {
                dataPage.setSort(" DESC ");
            }
            if (!StringUtils.hasText(dataPage.getDepartId())) {
                dataPage.setDepartId(userInfo.getDepartId());
            }
            PageInfo<UserQueryDTO> pageInfo = userService.userQuery(dataPage);
            result.setCode(0);
//            ArrayList<User> data = (ArrayList<User>) map.get("v_cursor1");
//            ArrayList<DataPage> pages = (ArrayList<DataPage>) map.get("v_cursor2");
//            DataPage dataPage1 = (DataPage) pages.get(0);
//            dataPage1.setData(data);
            Map<String, Object> map = new HashMap<>(2);
            map.put("total", pageInfo.getTotal());
            map.put("rows", pageInfo.getList());
            result.setData(map);

//            Map<String, Object> mapnew = new HashMap<>();
//            mapnew.put("total", dataPage1.gettotalRecords());
//            mapnew.put("rows", data);
//            result.setCode(ResultEnum.SUCCESS.getCode());
//            result.setData(mapnew);

            Result logResult = new Result();
            logResult.setCode(0);
            logResult.setMsg("数据获取成功");//不记录返回数据，只做标识
            logResult.setServerID(result.getServerID());
            logService.logAdd(dataPage.toString(), "获取用户列表", "SUCCESS", logResult.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(dataPage.toString(), "获取用户列表", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @RequestMapping(value = "/getUser")
    @ApiOperation(value = "获取用户数据")
    public Result GetUserMessNew(HttpServletRequest request, HttpServletResponse response) {
        UserInfo userInfo = new UserInfo();
        Result result = new Result();
        try {
            String token = request.getHeader("token").toString();
//            userInfo = (UserInfo) redisDao.vGet(token);
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Map<String, Object> requestMap = RequestParamerMap.getParamerMap(request);
            Map<String, Object> map = new HashMap<>();
            map.put("P_USER_ID", userInfo.getUserId());
            userInfo =Optional.ofNullable(userService.getUserMessNew(map)).orElse(userInfo);
            if(StrUtil.isEmpty(userInfo.getUserName())){
              userInfo.setUserName("游客");
            }
            if(StrUtil.isEmpty(userInfo.getDepartName())){
                userInfo.setDepartName("");
            }
            if(StrUtil.isEmpty(userInfo.getRoleName())){
                userInfo.setRoleName("");
            }
//            Volunteer volunteer = new Volunteer();
//            volunteer.setUserId(userInfo.getUserId());
//            volunteer.setState(3);
//            if(volunteerService.queryDataList(volunteer).size()>0){
//                userInfo.setVolunteerFlag("1");
//            }else{
//                userInfo.setVolunteerFlag("0");
//            }
            logService.logAdd(requestMap.toString(), "获取用户数据", "SUCCESS", map.toString(), userInfo, request);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(userInfo);
        } catch (Exception E) {
            result.setCode(ResultEnum.EREOR.getCode());
            userInfo.setUserName("游客");
            result.setData(userInfo);
        }
        return result;
    }

    /**
     * @Description: 获取用户角色列表
     * @Author: xy
     * @CreateDate: 2019/12/28 11:19
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:19
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/userRoleTableList")
    @ApiOperation(value = "获取用户角色列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result userRoleTableList(@RequestBody @ApiParam(value = "分页传入参数") DataPageVO dataPage, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
//            Map<String, Object> map = new HashMap<>();
//            map.put("P_PAGE", dataPage.getPage());
//            map.put("P_PAGESIZE", dataPage.getRows());
//            map.put("P_USER_ID", dataPage.getUserId());
            if (null == dataPage.getPage()) {
                dataPage.setPage(1);
            }
            if (null == dataPage.getRows()) {
                dataPage.setRows(20);
            }
//            dataPage.setUserId(userInfo.getUserId());
            PageInfo<UserRoleDepart> pageInfo = userService.userRoleTableList(dataPage);
            result.setCode(ResultEnum.SUCCESS.getCode());
//            ArrayList<UserRoleDepart> data = (ArrayList<UserRoleDepart>) map.get("v_cursor1");
//            ArrayList<DataPage> pages = (ArrayList<DataPage>) map.get("v_cursor2");
//            DataPage dataPage1 = (DataPage) pages.get(0);
            Map<String, Object> map = new HashMap<>(2);
            map.put("total", pageInfo.getTotal());
            map.put("rows", pageInfo.getList());
//            dataPage1.setData(data);
            result.setData(map);
            result.setCode(0);

//            Map<String, Object> mapnew = new HashMap<>();
//            mapnew.put("total", dataPage1.gettotalRecords());
//            mapnew.put("rows", data);
//            result.setCode(ResultEnum.SUCCESS.getCode());
//            result.setData(mapnew);

            Result logResult = new Result();
            logResult.setCode(0);
            logResult.setMsg("数据获取成功");//不记录返回数据，只做标识
            logResult.setServerID(result.getServerID());
            logService.logAdd(dataPage.toString(), "获取用户角色列表", "SUCCESS", logResult.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(dataPage.toString(), "获取用户角色列表", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/saveDepartRole")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "用户新增附属角色")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result saveDepartRole(@RequestBody @ApiParam(value = "用户信息") UserRoleDepart userRoleDepart, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Map<String, Object> map = new HashMap<>();
            map.put("V_USER_ID", userRoleDepart.getUserId());
            map.put("V_ROLE_ID", userRoleDepart.getRoleId());
            map.put("V_DEPART_ID", userRoleDepart.getDepartId());
            int userCount = userService.ValidUserDepartRole(map);
//            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("CUR");
//            if(list.size()>0){
            if (userCount > 0) {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("该用户已存在对应的部门和角色");
            } else {
                userRoleDepart.setCreateUser(userInfo.getUserId());
                boolean flag = userService.userRoleAdd(userRoleDepart);
                if (flag == true) {
                    result.setCode(ResultEnum.SUCCESS.getCode());
                    result.setMsg("提交成功");
                } else {
                    result.setCode(-1);
                    result.setMsg("提交失败");
                }
            }

            logService.logAdd(userRoleDepart.toString(), "用户新增附属角色", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(userRoleDepart.toString(), "用户新增附属角色", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 用户角色删除
     * @Author: xy
     * @CreateDate: 2019/12/28 11:25
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:25
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/changeRoleType")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "用户角色设置默认")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result changeRoleType(@RequestBody @ApiParam(value = "用户角色设置默认") UserRoleDepart userRoleDepart, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            boolean flag = userService.changeRoleType(userRoleDepart);
            if (flag == true) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("设置成功");
            } else {
                result.setCode(-1);
                result.setMsg("设置失败");
            }
            logService.logAdd(userRoleDepart.toString(), "用户角色设置默认", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(userRoleDepart.toString(), "用户角色设置默认", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 用户角色删除
     * @Author: xy
     * @CreateDate: 2019/12/28 11:25
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:25
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/UserRoleDelete")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "用户角色删除")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result UserRoleDelete(@RequestBody @ApiParam(value = "用户角色信息") UserRoleDepart userRoleDepart, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            boolean flag = userService.userRoleDelete(userRoleDepart);
            if (flag == true) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("删除成功");
            } else {
                result.setCode(-1);
                result.setMsg("删除失败,默认用户不可删除");
            }
            logService.logAdd(userRoleDepart.toString(), "用户角色删除", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(userRoleDepart.toString(), "用户角色删除", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 用户新增
     * @Author: xy
     * @CreateDate: 2019/12/28 11:37
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:37
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/save")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "用户新增/编辑")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result UserAdd(@RequestBody @ApiParam(value = "用户信息") User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            user.setCreateUser(userInfo.getUserId());
//            Base64EnOut base64EnOut = new Base64EnOut();
//            user.setPassword(base64EnOut.Decode(user.getPassword()));
            boolean flag = userService.userAdd(user);
            if (flag == true) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("提交成功");
            } else {
                result.setCode(-1);
                result.setMsg("提交失败,存在相同用户名！");
            }
            logService.logAdd(user.toString(), "用户新增/编辑", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(user.toString(), "用户新增/编辑", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 用户删除
     * @Author: xy
     * @CreateDate: 2019/12/26 16:24
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/26 16:24
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "用户删除")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result UserDelete(@RequestBody @ApiParam(value = "功能消息") User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            boolean flag = userService.userDelete(user);
            if (flag) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("删除成功");
            } else {
                result.setCode(-1);
                result.setMsg("删除失败");
            }
            logService.logAdd(user.toString(), "用户删除", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(user.toString(), "用户删除", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 修改用户密码
     * @Author: xy
     * @CreateDate: 2019/12/28 11:40
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:40
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/modifyPwd")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "修改用户密码")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result UserPwdUpdate(@RequestBody @ApiParam(value = "用户信息") ModifyUser user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        Base64EnOut base64EnOut = new Base64EnOut();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Map<String, Object> map = new HashMap<>();
            map.put("V_USER_ID", userInfo.getUserId());
            map.put("V_PASSWORD_OLD", base64EnOut.Decode(user.getOldPassword()));
            int userCount = userService.ValidUserOldPwd(map);
//            List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("v_cursor");
//            if(list.size()>0){
            if (userCount > 0) {
                Map<String, Object> mapnew = new HashMap<>();
                mapnew.put("P_PASSWORD", base64EnOut.Decode(user.getNewPassword()));
                mapnew.put("P_USER_ID", userInfo.getUserId());
                boolean flag = userService.userPwdUpdate(mapnew);
                if (flag == true) {
                    result.setCode(ResultEnum.SUCCESS.getCode());
                    result.setMsg("修改成功");
                } else {
                    result.setCode(-1);
                    result.setMsg("修改失败");
                }
                logService.logAdd(user.toString(), "修改用户密码", "SUCCESS", result.toString(), userInfo, request);
            } else {
                result.setCode(ResultEnum.EREOR.getCode());
                result.setMsg("原密码错误");
            }

        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(user.toString(), "修改用户密码", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }


    @PostMapping(value = "/updatePassword")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "用户管理修改用户密码")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result updatePassword(@RequestBody @ApiParam(value = "用户信息") ModifyUser user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        Base64EnOut base64EnOut = new Base64EnOut();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Map<String, Object> mapnew = new HashMap<>();
            mapnew.put("P_PASSWORD", base64EnOut.Decode(user.getNewPassword()));
            mapnew.put("V_USER_ID", user.getUserId());
            boolean flag = userService.updatePassword(mapnew);
            if (flag == true) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("修改成功");
            } else {
                result.setCode(-1);
                result.setMsg("修改失败");
            }
            logService.logAdd(user.toString(), "用户管理修改用户密码", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(user.toString(), "用户管理修改用户密码", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/updatePwdNotoken")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "用户管理修改用户密码")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result updatePwdNotoken(@RequestBody @ApiParam(value = "用户信息") ModifyUser user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        Base64EnOut base64EnOut = new Base64EnOut();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        Map<String, Object> mapnew = new HashMap<>();
        mapnew.put("P_PASSWORD", base64EnOut.Decode(user.getNewPassword()));
        mapnew.put("V_USER_ID", base64EnOut.Decode(user.getUserId()));
        boolean flag = userService.updatePassword(mapnew);
        if (flag == true) {
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("修改成功");
        } else {
            result.setCode(-1);
            result.setMsg("修改失败");
        }
        logService.logAdd(user.toString(), "用户管理修改用户密码", "SUCCESS", result.toString(), userInfo, request);
        return result;
    }

    @PostMapping(value = "/modifySkin")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "切换主题")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result modifySkin(@RequestBody @ApiParam(value = "用户信息") UserInfo user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            user.setUserId(userInfo.getUserId());
            Map<String, Object> map = new HashMap<>();
            map.put("P_USER_ID", userInfo.getUserId());
            map.put("P_USER_SKIN", user.getUserSkin());
            boolean flag = userService.modifySkin(map);
            if (flag == true) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("切换成功");
            } else {
                result.setCode(-1);
                result.setMsg("切换失败");
            }
            logService.logAdd(user.toString(), "切换主题", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(user.toString(), "切换主题", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }


    /**
     * @Description: 工号解锁
     * @Author: xy
     * @CreateDate: 2019/12/28 11:43
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:43
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/UserJieSuo")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "工号解锁")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result userJieSuo(@RequestBody @ApiParam(value = "用户信息") User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            user.setCreateUser(userInfo.getUserId());
            Map<String, Object> map = new HashMap<>();
            map.put("P_ID", user.getId());
            boolean flag = userService.userJieSuo(map);
            if (flag == true) {
                result.setCode(200);
                result.setMsg("解锁成功");
            } else {
                result.setCode(-1);
                result.setMsg("解锁失败");
            }
            logService.logAdd(user.toString(), "工号解锁", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(user.toString(), "工号解锁", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 切换角色
     * @Author: xy
     * @CreateDate: 2019/12/28 11:43
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/28 11:43
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/UserRoleChange")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "角色切换")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result UserRoleChange(@RequestBody @ApiParam(value = "用户角色信息") UserRoleDepart userRoleDepart, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        UserInfo userInfonew = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfonew = JwtUtil.getUserInfoFromToken(token, redisDao);
            Map<String, Object> map = new HashMap<>();
            map.put("V_UDR_ID", userRoleDepart.getId());
            List<UserInfo> info = userService.UserRoleChange(map);
//            ArrayList<UserInfo> info = (ArrayList<UserInfo>) map.get("v_cursor");
            userInfo = info.get(0);
            String userId = userInfo.getUserName();
            byte[] b = userId.getBytes("UTF-8");
            userId = new String(b, "UTF-8");
            String roleId = userInfo.getRoleName();
            byte[] c = roleId.getBytes("UTF-8");
            roleId = new String(c, "UTF-8");
            String departId = userInfo.getDepartName();
            byte[] d = departId.getBytes("UTF-8");
            departId = new String(d, "UTF-8");
            String skin = userInfo.getUserSkin();
            byte[] s = skin.getBytes("UTF-8");
            skin = new String(s, "UTF-8");
            String token1 = JwtUtil.createToken(userInfo.getUserId(), userId, userInfo.getDepartId(), userInfo.getRoleId(), roleId, departId, userInfo.getRealAccNbr(), userInfo.getAreaCode(), "", "", "", "", skin);
            redisDao.vSet(token1, userInfo, 600 * 60L);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(token1);
            result.setMsg("角色切换成功");
            logService.logAdd(userRoleDepart.toString(), "角色切换", "SUCCESS", result.toString(), userInfonew, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(userRoleDepart.toString(), "角色切换", "ERROR", result.toString(), userInfonew, request);
        }
        return result;
    }

    @PostMapping(value = "/userStop")
    @ResponseBody
    @ApiOperation(value = "用户停用/启用")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result userStop(@RequestBody @ApiParam(value = "用户ID") User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            boolean flag = userService.userStop(user);
            if (flag) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("操作成功");
            } else {
                result.setCode(-1);
                result.setMsg("操作失败");
            }
            logService.logAdd(user.toString(), "用户停用/启用", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(user.toString(), "用户停用/启用", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getUserListByDId")
    @ApiOperation(value = "获取用户列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result userQueryByDeId(@RequestBody @ApiParam(value = "分页传入参数") DataPageVO dataPage, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token,redisDao);
            if (null == dataPage.getPage()) {
                dataPage.setPage(1);
            }
            if (null == dataPage.getRows()) {
                dataPage.setRows(20);
            }
            if(!StringUtils.hasText(dataPage.getSort())){
                dataPage.setSort(" CREATE_TIME ");
            }
            if(!StringUtils.hasText(dataPage.getOrder())){
                dataPage.setSort(" DESC ");
            }
            if(!StringUtils.hasText(dataPage.getDepartId()))
            {
                dataPage.setDepartId(userInfo.getDepartId());
            }
            PageInfo<UserQueryDTO> pageInfo=userService.userQueryByDId(dataPage);
            result.setCode(0);
            Map<String, Object> map = new HashMap<>(2);
            map.put("total", pageInfo.getTotal());
            map.put("rows", pageInfo.getList());
            result.setData(map);
            Result logResult = new Result();
            logResult.setCode(0);
            logResult.setMsg("数据获取成功");//不记录返回数据，只做标识
            logResult.setServerID(result.getServerID());
            logService.logAdd(dataPage.toString(), "获取用户列表", "SUCCESS", logResult.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(dataPage.toString(), "获取用户列表", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation("获取用户信息通过部门树")
    @GetMapping("/getUsersByDepartTree")
    public Result getUsersByDepartTree(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            List<Map<String,Object>> users = userService.getUsersByDepartTree("0");
            result.setCode(200);
            result.setMsg("获取成功");
            result.setData(users);
            logService.logAdd(null, "获取所有用户信息", "SUCCESS", result.toString(), userInfo, request);
        }else{
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取所有用户信息", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation("通过角色分类获取用户信息")
    @GetMapping("/getUsersByRole")
    public Result getUsersByRole(HttpServletRequest request){
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);

            List<Map<String,Object>> users = userService.getUsersByRole();

            result.setCode(200);
            result.setMsg("获取成功");
            result.setData(users);
            logService.logAdd(null, "获取所有用户信息", "SUCCESS", result.toString(), userInfo, request);
        }else{
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取所有用户信息", "ERROR", result.toString(), userInfo, request);
        }
        return result;

    }

}
